﻿using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;

namespace Test.EntityFramework.Core
{
    [AppDbContext("DbConnectionString", DbProvider.Sqlite)]
    public class DefaultDbContext : AppDbContext<DefaultDbContext>
    {
        public DefaultDbContext(DbContextOptions<DefaultDbContext> options) : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //#region 采用自带模式能够正常生成, 需屏蔽接口IEntityTypeBuilder<A>和IEntityTypeBuilder<B>
            //modelBuilder.Entity<A>().Property(c => c.A1).IsRequired().HasMaxLength(50);
            //modelBuilder.Entity<B>().Property(c => c.B1).IsRequired().HasMaxLength(50);
            //#endregion
        }
    }

    public class A : Entity<Guid>, IEntityTypeBuilder<A>
    {
        public virtual String A1 { get; set; }
        public void Configure(EntityTypeBuilder<A> builder, DbContext dbContext, Type dbContextLocator)
        {
            builder.Property(c => c.A1).IsRequired().HasMaxLength(50);
        }
    }

    public class B : A, IEntityTypeBuilder<B>
    {
        public virtual String B1 { get; set; }
        public void Configure(EntityTypeBuilder<B> builder, DbContext dbContext, Type dbContextLocator)
        {
            builder.Property(c => c.B1).IsRequired().HasMaxLength(50);
        }
    }
}